{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "40ac744f",
   "metadata": {},
   "source": [
    "# Caffe Concat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a57ebe23",
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from tvm_book.config import env\n",
    "# 设置 caffeprotobuf环境\n",
    "env.set_caffeproto(Path(env.__file__).parents[3]/\"tests/caffeproto\")\n",
    "# 设置tvm环境\n",
    "env.set_tvm(\"/media/pc/data/board/arria10/lxw/tasks/tvm-test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ecf702f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from google.protobuf import text_format\n",
    "import caffe_pb2 as pb2\n",
    "\n",
    "temp_dir = Path(\".temp\")\n",
    "temp_dir.mkdir(exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2fab34d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "layer {\n",
    "  name: \"data0\"\n",
    "  type: \"Input\"\n",
    "  top: \"data0\"\n",
    "  input_param {\n",
    "    shape {\n",
    "      dim: 1\n",
    "      dim: 3\n",
    "      dim: 10\n",
    "      dim: 10\n",
    "    }\n",
    "  }\n",
    "}\n",
    "layer {\n",
    "  name: \"output\"\n",
    "  type: \"Concat\"\n",
    "  bottom: \"data0\"\n",
    "  bottom: \"data1\"\n",
    "  top: \"output\"\n",
    "  concat_param {\n",
    "    axis: 1\n",
    "  }\n",
    "}\n",
    "layer {\n",
    "  name: \"data1\"\n",
    "  type: \"Input\"\n",
    "  top: \"data1\"\n",
    "  input_param {\n",
    "    shape {\n",
    "      dim: 1\n",
    "      dim: 2\n",
    "      dim: 10\n",
    "      dim: 10\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\n",
    "\"\"\"\n",
    "predict_net = text_format.Merge(text, pb2.NetParameter())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af3f7cbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data0': data0, 'data1': data1, 'output': R.concat((data0, data1), axis=1)}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tvm.relax.testing import nn\n",
    "from tvm.relax import op as _op\n",
    "exp_tab = {} # 存储节点\n",
    "dtype = \"float32\"\n",
    "# 优先处理输入层\n",
    "for pl in predict_net.layer:\n",
    "    name = pl.name\n",
    "    if pl.type == \"Input\":\n",
    "        shape = pl.input_param.shape\n",
    "        assert len(shape)==1 and len(pl.top)==1, \"Input 类型仅仅支持单输入单输出\"\n",
    "        shape = list(shape[0].dim)\n",
    "        exp_tab[name] = nn.Placeholder(shape, dtype, name)\n",
    "for pl in predict_net.layer:\n",
    "    name = pl.name\n",
    "    if pl.type == \"Concat\":\n",
    "        inputs = [exp_tab[name] for name in pl.bottom]\n",
    "        exp_tab[name] = _op.concat(inputs, axis=pl.concat_param.axis)\n",
    "exp_tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df9246e8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py313",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
